package com.mck.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.mck.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/monitor")
public class DruidController {

    @Autowired
    private DruidDataSource druidDataSource;

    @GetMapping("/stat")
    public Result druidStat() {
        try {
            Map<String, Object> info = new HashMap<>();
            
            // 只获取关键信息
            info.put("activeCount", druidDataSource.getActiveCount());
            info.put("poolingCount", druidDataSource.getPoolingCount());
            info.put("url", druidDataSource.getUrl());
            info.put("username", druidDataSource.getUsername());
            info.put("driverClassName", druidDataSource.getDriverClassName());
            
            return Result.success(info);
        } catch (Exception e) {
            log.error("获取数据源信息失败", e);
            return Result.error("获取数据源信息失败: " + e.getMessage());
        }
    }
}